<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc fo">

  <xsl:output method="xml" media-type="application/pdf"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="document('/test.livcos.org/data/playground.view')/*"/>
  </xsl:template>
  
  <xsl:template match="doc:document">
    <root>
      <layout-master-set>
        <simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm"
          margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
          <region-body margin-top="3cm" />
          <region-before extent="3cm" />
          <region-after extent="1.5cm" />
        </simple-page-master>
      </layout-master-set>

      <page-sequence master-reference="A4">

        <static-content flow-name="xsl-region-before">
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="33%"/> 
            <fo:table-column column-width="33%"/>
            <fo:table-column column-width="33%"/>
            <fo:table-body>
              <fo:table-row>
                <fo:table-cell padding="2pt">
                  <block font-size="12pt" font-family="sans-serif" text-align="start" color="blue">
                    <xsl:value-of select="doc:head/doc:title"/>
                  </block>
                </fo:table-cell>
                <fo:table-cell padding="2pt">
                  <block font-size="12pt" font-family="sans-serif" text-align="center" color="blue">
                    <xsl:value-of select="doc:head/doc:title"/>
                  </block>
                </fo:table-cell>
                <fo:table-cell padding="2pt">
                  <block font-size="12pt" font-family="sans-serif" text-align="end" color="blue">
                    <xsl:value-of select="doc:head/doc:context"/>
                  </block>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-body>
          </fo:table>
        </static-content>
    
        <static-content flow-name="xsl-region-after">
          <block text-align="end">Page <page-number/></block>
        </static-content>

        <flow flow-name="xsl-region-body">
          <fo:block background-color="blue" color="white" text-align="center">
            <xsl:value-of select="doc:head/doc:context"/>
          </fo:block>

          <block text-align="center" margin-top="1cm" margin-bottom="1cm">
            <external-graphic src="../../image/default.jpg"
              width="100%" content-width="scale-to-fit" scaling="uniform" content-height="100%"/>
          </block>
          <block text-align="center" margin-top="1cm" margin-bottom="1cm">
            <external-graphic
              src="../../image/default"
              width="100%" content-width="scale-to-fit" scaling="uniform" content-height="100%" />
          </block>
          <block font-size="12pt" font-family="sans-serif" line-height="15pt"
            space-after.optimum="3pt" text-align="justify">
            The Extensible Markup Language (XML) is a subset of SGML that is completely described in
            this document. Its goal is to enable generic SGML to be served, received, and processed
            on the Web in the way that is now possible with HTML. XML has been designed for ease of
            implementation and for interoperability with both SGML and HTML.
          </block>
          
          <block text-align="center" margin-top="1cm" margin-bottom="1cm">
            <external-graphic
              src="../../image/pdf.gif"
              content-width="10cm"/>
          </block>
          <xsl:apply-templates select="doc:body/*"/>

        </flow>
        
      </page-sequence>
    </root>
  </xsl:template>

  <xsl:template match="doc:chapter">
    <block font-size="18pt" font-family="sans-serif" line-height="22pt"
      space-after.optimum="3pt">
      <xsl:value-of select="doc:title"/>
    </block>
    <xsl:apply-templates select="*[name(.)!='title']"/>
  </xsl:template>

  <xsl:template match="doc:paragraph">
    <block font-size="12pt" font-family="sans-serif" line-height="15pt"
      space-after.optimum="3pt" text-align="justify">
      <xsl:apply-templates/>
    </block>
  </xsl:template>

</xsl:stylesheet>
